package com.neudeu.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.neudeu.dao.IUserDAO;
import com.neudeu.entity.UserEntity;
import com.neudeu.util.ResultHandler;
import com.neudeu.util.SqlQueryUpdate;

public class UserDAOImpl implements IUserDAO{

	@Override
	public List<UserEntity> getAll() {
		
		return null;
	}

	@Override
	public List<UserEntity> getByLevel(int ulevel) {
		String sql="select * from usertable where ulevel=?";
		return SqlQueryUpdate.query(sql,new ResultHandler<UserEntity>() {

			@Override
			public UserEntity getResult(ResultSet rs) throws SQLException {
				UserEntity user=new UserEntity();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				
				return user;
			}
		}, ulevel);
	}

	@Override
	public int userCount(int ulevel) {
		String sql="SELECT count(*) from usertable where ulevel=?";
		List<Integer> listcount=SqlQueryUpdate.query(sql, new ResultHandler<Integer>() {

			@Override
			public Integer getResult(ResultSet rs) throws SQLException {
				
				return rs.getInt(1);
			}
		}, ulevel);
		return listcount.get(0);
	}

	@Override
	public List<UserEntity> getByLevelLimit(int ulevel, int offset, int num) {
		
		String sql="select * from usertable where ulevel=? limit ?,?";
		return SqlQueryUpdate.query(sql,new ResultHandler<UserEntity>() {

			@Override
			public UserEntity getResult(ResultSet rs) throws SQLException {
				UserEntity user=new UserEntity();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				
				return user;
			}
		}, ulevel,offset,num);
	}

}
